<?php

namespace Biz\model\bizAd;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class AdPlanFlowRate extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_biz_ad';

    const _CONFIG_SELECT_ = 'db_biz_ad';

    const _DATABASE_ = 'biz_ad';

    const _TABLE_NAME = 'hhz_ad_flow_rate';

    const DEFAULT_RATE = 1;
    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    use BizModelTrait;

    public static function getUserFlowRate($uid = 0)
    {
        $userRate = 0;
        $commonRate = self::DEFAULT_RATE;
        $where = [
            'uid' => 0,
            'status' => self::STATUS_NORMAL
        ];
        if ($uid > 0) {
            $where['uid'] = [0, (int)$uid];
        }
        $list = self::getList($where);
        foreach ($list as $row) {
            if ($row['uid'] == 0) {
                $commonRate = $row['flow_rate'];
            }
            if ($row['uid'] == $uid) {
                $userRate = $row['flow_rate'];
            }
        }
        $userRate = $userRate ?: $commonRate;
        return $userRate;
    }

    public static function getInfoById($id)
    {
        $info = self::dataSelectInfo(['id' => $id]);
        return $info;
    }

    public static function getInfo($uid = 0)
    {
        $info = self::dataSelectInfo(['uid' => $uid]);
        return $info;
    }

    public static function getList($where)
    {
        $res = self::dataSelect($where);
        return $res;
    }

    public static function addData($data)
    {
        $data['addtime'] = date('Y-m-d H:i:s');
        $data['edittime'] = date('Y-m-d H:i:s');
        $id = self::dataInsert($data);
        return $id;
    }

    public static function updateData($id, $data)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        return self::dataUpdate($data, ['id' => $id]);
    }

}